﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using Editor.PageControls;
using Editor.Utilities;

namespace Editor
{
    public partial class ButtonHideProperties : PropertiesFormBase, IDictSubject<string>
    {
        public ButtonHideProperties()
        {
            InitializeComponent();
        }

        protected override void PropertiesForm_Load(object sender, EventArgs e)
        {
            base.PropertiesForm_Load(sender, e);

            PageHideButton selectedHiddenButton = PageControl.SelectedControl as PageHideButton;

            ControlName = selectedHiddenButton.Name;
            comboBox1.Items.Clear();
            comboBox1.Items.AddRange(EditorForm.PageControls);

            string hideControl = ((PageHideButton)selectedHiddenButton).HideControl;

            if (hideControl != null && !hideControl.Equals(string.Empty))
                comboBox1.SelectedIndex = comboBox1.Items.IndexOf(hideControl);

            Dictionary = selectedHiddenButton.Dictionary;

            if (comboBoxLanguage.SelectedIndex == -1)
                comboBoxLanguage.Items.AddRange(EditorForm.SupportedLanguages);

            resetSubject();
            textBoxText.Text = selectedHiddenButton.Dictionary[EditorForm.CurrentLanguage];
            comboBoxLanguage.SelectedIndex = EditorForm.CurrentLanguageIndex;

            textBoxName.Text = selectedHiddenButton.Name;
            textBoxFont.Text = selectedHiddenButton.TextFontSize.ToString();
            textBoxX.Text = selectedHiddenButton.Location.X.ToString();
            textBoxY.Text = selectedHiddenButton.Location.Y.ToString();
        }

        protected override void buttonApply_Click(object sender, EventArgs e)
        {
            PageHideButton selectedHideButton = PageControl.SelectedControl as PageHideButton;

            if (textBoxName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("İsim girmelisiniz!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (!selectedHideButton.Name.Equals(textBoxName.Text.Trim()) &&
                !EditorForm.IsElementNameAvailable(textBoxName.Text.Trim()))
            {
                MessageBox.Show("Bu isim zaten var!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (textBoxText.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Yazı girmelisiniz!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            int x = -1, y = 1;

            if (!int.TryParse(textBoxX.Text, out x) || x < 0)
            {
                MessageBox.Show("X değeri pozitif bir tam sayı olmalı!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (!int.TryParse(textBoxY.Text, out y) || y < 0)
            {
                MessageBox.Show("Y değeri pozitif bir tam sayı olmalı!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            float fontSize = -1f;

            if (!float.TryParse(textBoxFont.Text, System.Globalization.NumberStyles.Float, CultureInfo.CurrentCulture,
                out fontSize) || fontSize <= 0)
            {
                MessageBox.Show("Font büyüklüğü kesirli bir sayı olmalı!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            selectedHideButton.Name = textBoxName.Text.Trim();
            string selectedLanguage = comboBoxLanguage.SelectedItem as string;

            Dictionary[selectedLanguage] = textBoxText.Text;
            selectedHideButton.Dictionary = Dictionary;
            //selectedHideButton.Text = textBoxText.Text;
            selectedHideButton.Location = new Point(x, y);
            selectedHideButton.HideControl = (string)comboBox1.SelectedItem;
            selectedHideButton.TextFontSize = fontSize;

            base.buttonApply_Click(sender, e);
        }

        protected override void buttonRemove_Click(object sender, EventArgs e)
        {
            base.buttonRemove_Click(sender, e);
        }

        protected override void buttonCopy_Click(object sender, EventArgs e)
        {
            base.buttonCopy_Click(sender, e);
        }

        protected override void buttonRelocate_Click(object sender, EventArgs e)
        {
            base.buttonRelocate_Click(sender, e);
        }

        public SDictionary<string> Dictionary { get; set; }

        private void comboBoxLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            SDictionary<string> dict = Dictionary;
            textBoxText.Text = changeSubject(comboBoxLanguage.SelectedItem as string,
                comboBoxLanguage.SelectedIndex, textBoxText.Text, ref dict);
            Dictionary = dict;
        }
    }
}
